package com.lb.trac.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.lb.trac.pojo.Societa;
import com.lb.trac.pojo.Utenti;
import com.lb.trac.pojo.condition.FindSocietaByCodClienteHRPO;

public class HRPOSearchService {

	private JdbcTemplate jdbcTemplate;
	private SearchService searchService;
	private String sql;

	public Utenti[] findUtentiHrpo(){
		
			List<Utenti> utentis  = getJdbcTemplate().query(getSql(), new RowMapper<Utenti>(){

				@Override
				public Utenti mapRow(ResultSet rs, int i)
						throws SQLException {
					Utenti u = new Utenti();
					u.setNome(rs.getString("NOMINATIVO_COMPLETO"));
					u.setEmail(rs.getString("EMAIL"));
					Societa[] societa = searchService.find(new Societa[0], new FindSocietaByCodClienteHRPO(rs.getString("COD_CLIENTE")), Societa.class);
					u.setSocieta((societa!=null && societa.length>0)?societa[0]:null);
					u.setUsername(rs.getString("LOGIN_UTENTE"));
					return u;
				}
				
			});
			return utentis.toArray(new Utenti[0]);
		
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}


	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

}
